package com.db.client;

import com.db.protocol.ClientProtocol;
import com.db.protocol.ReportFixProtocol;
import com.db.protocol.message.LogonClientMessage;
import com.db.transport.SessionFactory;
import com.db.transport.client.SessionClient;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.util.logging.Logger;

/**
 * @author nulls.narik
 */
public class Client {

    private static Logger logger = Logger.getLogger(Client.class.getName());
    public static Long TIMER = 5000L;

    public String getClientID() {
        return clientID;
    }

    private final String clientID;
    private final Integer typeAggregation;
    private Thread threadSession;
    private SessionClient session;

    public Client(String clientID, Integer typeAggregation) {
        this.clientID = clientID;
        this.typeAggregation = typeAggregation;
    }

    public void connect(InetAddress address, int port) {
        try {
            session = SessionFactory.getSessionClient(clientID, address, port);
            session.addListener(new ClientListener(new PrintWriter(System.out, true)));
            threadSession = new Thread(session);
            threadSession.start();
            session.sendMessage(clientID);
            session.sendMessage(typeAggregation.toString());
            session.sendMessage(TIMER.toString());
            logger.info(String.format("Client [%s]: start transport part", clientID));
        } catch (IOException e) {
            logger.warning(String.format("Client [%s]: error create transport part", clientID));
        }
    }

}
